using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;


using System.Data.Linq;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;

using DataLayer;

namespace BusinessLayer.Activities
{
   public partial class Update_WidgetInstanceProperties : System.Workflow.ComponentModel.Activity
   {
      public Update_WidgetInstanceProperties()
      {
         //InitializeComponent();
      }

      private static DependencyProperty WidgetInstanceIdProperty = DependencyProperty.Register("WidgetInstanceId", typeof(int), typeof(Update_WidgetInstanceProperties));
      public int WidgetInstanceId
      {
         get { return (int)base.GetValue(WidgetInstanceIdProperty); }
         set { base.SetValue(WidgetInstanceIdProperty, value); }
      }

      private static DependencyProperty wPropertiesProperty = DependencyProperty.Register("wProperties", typeof(Dictionary<string, string>), typeof(Update_WidgetInstanceProperties));
      public Dictionary<string, string> wProperties
      {
         get { return (Dictionary<string, string>)base.GetValue(wPropertiesProperty); }
         set { base.SetValue(wPropertiesProperty, value); }
      }

      protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
      {
         using (var db = DatabaseHelper.GetDashboardData())
         {
            var w = db.TabWidgets.Single(p => p.Id == WidgetInstanceId);


            XElement main = new XElement("state");
            foreach(var key in wProperties.Keys)
            {
               XElement xKey = new XElement(key);
               xKey.Value = wProperties[key];
               main.Add(xKey);
            }
            w.State = main.ToString(SaveOptions.DisableFormatting);

            db.SubmitChanges();
         }
         return ActivityExecutionStatus.Closed;
      }
   }
}